package com.chat.system.controller;

import com.chat.system.entity.domain.Res;
import com.chat.system.entity.dto.ApplicationDTO;
import com.chat.system.entity.dto.ChatUserDTO;
import com.chat.system.entity.po.ChatUserApplication;
import com.chat.system.service.IChatUserApplicationService;
import com.chat.system.utils.UserUtil;
import com.github.javafaker.App;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author czh
 * @since 2023-10-28
 */
@RestController
@RequestMapping("/chatUserApplication")
public class ChatUserApplicationController {
    @Autowired
    IChatUserApplicationService chatUserApplicationService;

    @PostMapping("friendApplication")
    public Res friendApplication (@RequestBody ChatUserDTO chatUserDTO) throws IOException {
        final String currentUserName = UserUtil.getCurrentUserName();
        final boolean res = chatUserApplicationService.addNewApplication(currentUserName, chatUserDTO.getUserName());
        return res ? Res.success() : Res.error();
    }

    @PostMapping("respondToFriendApplication")
    public Res respondToFriendApplication (@RequestBody Map<String ,String> params) {
        final String id = params.get("id");
        final String status = params.get("status");
        chatUserApplicationService.confirm(id,status);
        return Res.success();
    }

    @GetMapping("getApplications")
    public Res getApplication() {
        final String currentUserName = UserUtil.getCurrentUserName();
        List<ApplicationDTO> list =  chatUserApplicationService.getApplication(currentUserName);
        final Res success = Res.success();
        success.put("list",list);
        return success;
    }
}
